﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Fayshine.VA.RIAService.Web;

namespace Fayshine.VA.Views.SysConfig
{
    public partial class UserWindow : ChildWindow
    {
        private user m_User = null;
        public user User
        {
            get
            {
                return m_User;
            }
            set
            {
                m_User = value;
            }
        }

        public UserWindow(string title)
        {
            InitializeComponent();
            this.Title = title;
            this.User = new user();
            this.User.RoleID = 1;
            this.LayoutRoot.DataContext = this.User;
            this.lblWarning.Visibility = System.Windows.Visibility.Collapsed;
        }

        public UserWindow(string title,user updateUser)
        {
            InitializeComponent();
            this.Title = title;
            this.User = updateUser;
            this.LayoutRoot.DataContext = this.User;
            this.lblWarning.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            string warning = string.Empty;
            if (string.IsNullOrEmpty(m_User.Password))
            {
                warning = "错误：密码不能为空！";
            }

            if (!string.IsNullOrEmpty(m_User.Password) && !m_User.Password.Equals(txtPwdConfirm.Password, StringComparison.OrdinalIgnoreCase))
            {
                warning = "错误：密码不一致！";
            }

            if (string.IsNullOrEmpty(m_User.CHNName))
            {
                warning = "错误：真实姓名不能为空！";
            }

            if (string.IsNullOrEmpty(m_User.Name))
            {
                warning = "错误：用户名不能为空！";
            }

            if (!string.IsNullOrEmpty(warning))
            {
                this.lblWarning.Text = warning;
                this.lblWarning.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                this.lblWarning.Visibility = System.Windows.Visibility.Collapsed;
                this.DialogResult = true;
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void txtPwdConfirm_LostFocus(object sender, RoutedEventArgs e)
        {
            if (m_User.Password == null || !m_User.Password.Equals(txtPwdConfirm.Password, StringComparison.OrdinalIgnoreCase))
            {
                lblWarning.Text = "错误：密码不一致！";
                lblWarning.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                lblWarning.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
    }
}

